<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;
use common\server\BasicDataServer;

/* @var $this yii\web\View */
/* @var $model common\models\AdminModel */

?>

<div class="admin-model-create">


	
    <?php $form = ActiveForm::begin(); ?>

    <?= $form->field($model, 'name')->textInput(['autofocus' => true])->label('角色称呼') ?>
    <?= $form->field($model, 'des')->textInput()->label('介绍') ?>
	<?= $form->field($model, 'rule')->textInput(['type'=>'hidden','id'=>'ruleid'])->label(false);?>
<!--//		<?= $form->field($model, 'rule')->textInput(['id'=>'ruleid'])->label('无聊');?>-->
		
	<?php 
		
		$htmls = "";
		foreach($rules as $k=>$v){
			
			$htmls .= "<label><input type='checkbox' value='{$k}' id='parent_{$k}'/> {$v['names']} </label><br>";
			
			$ruleArr  = $v['arr'];
			
			foreach($ruleArr as $key=>$value){
				$htmls .= "<label>&nbsp;&nbsp;&nbsp;&nbsp;<input name='rule_{$k}' type='checkbox' id='child_{$value['id']}' 
					value='{$value['id']}' /> {$value['title']} </label>";
			}
			$htmls .= "<br>";
			$htmls .= "<br>";
			
		}
		
		echo $htmls;
		
		
	?>
	
	<?php 
		
		$this->registerJs('var ruleArr = {};
		
		var curRuleArr = $("#ruleid").val().split(",");
		
		console.log(curRuleArr);
		
		var childid = 0;
		var parchildname = "";
		var childname = "";
		for(var i = 0;i<curRuleArr.length;i++){
			childid = curRuleArr[i];
			ruleArr[childid] = true;
			childname = "child_"+childid;
			parchildname = "parent_"+childid;
			
			if($("#"+childname)){
				$("#"+childname).prop("checked",true);
			}
			
			if($("#"+parchildname)){
				$("#"+parchildname).prop("checked",true);
			}
			
		}
		
		$(":checkbox").on("click", function () {
		    
		    var keyname = $(this).attr("id");
		    var keyArr = keyname.split("_");
		    var keys = keyArr[1];
		    var keytype = keyArr[0];
		    
		    var isselect = $(this).prop("checked");
		    
		    if(keytype == "child"){		    	
		    	ruleArr[keys] = isselect;
				
				if(isselect){
		    		var parName = $(this).attr("name");
		    		parNameArr = parName.split("_");
		    		var parname = "parent_"+parNameArr[1];
		    		$("#"+parname).prop("checked",isselect);
					
					ruleArr[parNameArr[1]] = isselect;
		    	}
				
		    }
		    else if(keytype == "parent"){
		    	var namesstr = "rule_"+keys;
				
				ruleArr[keys] = isselect;
				
			    $("[name="+namesstr+"]").each(function(index,el){
			    	
			    	var childid = $(el).attr("id");			    	
			    	var selectid = childid.split("_")[1];	
			    	    	
			    	$(el).prop("checked",isselect);
			    	ruleArr[selectid] = isselect;
			    });
		    }
		    
		    var checkStr = "";
		    for(var i in ruleArr)
            {
            	if(ruleArr[i] == true){
            		checkStr += i+",";
            	}
            }
            
            $("#ruleid").val(checkStr);
            console.log("checkStr="+checkStr);
		    
	});');
		
			
	?>
	
    <div class="form-group">
        <?= Html::submitButton('添加', ['class' => 'btn btn-primary', 'name' => 'signup-button']) ?>
    </div>

    <?php ActiveForm::end(); ?>

</div>
